<?php
/**
 * Created by PhpStorm.
 * Author: D H
 * Date: 2017/6/23
 * Time: 10:29
 */
namespace app\api\logic;

use think\Db;
use tool\Common;
use think\Request;

class Fire extends Base{

    //得到消防宝典分类
    public function getBibleType(){
        $list = Db::view('fire_type',['id' => 'typeID','name' => 'typeName'])->select();
        return Common::rm(1,'操作成功',['list' => $list]);
    }

    //根据类型ID得到消防宝典视频列表
    public function getBibleListByTypeID(){
        //测试数据
       /* $arr = ' {
             "typeID": 2
        }';
        $data = json_decode($arr,true);*/
        $data = $this->data;
        $list = Db::view('fire_bible',['id' => 'fireID','title','img'])->where('typeID',$data['typeID'])->select();
        if ($list){
            return Common::rm(1,'操作成功',['list' => $list]);
        }else{
            return Common::rm(-3,'没有数据');

        }


    }

    //根据消防ID得到消防宝典详细信息
    public function getFireMsgByFireID(){
        //测试数据
        /*$arr = ' {
           "fireID": 1
        }';
        $data = json_decode($arr,true);*/
        $data = $this->data;
        $msg = Db::view('fire_bible',['title','content','addtime','video'])
            ->where('id',$data['fireID'])
            ->find();
        $list = Db::view('fire_comments',['userID','content' => 'comments','addtime','praise'])
            ->view('operator','name,photo','operator.id= fire_comments.userID')
            ->where('fire_comments.fireID',$data['fireID'])
            ->where('parentID',1)
            ->limit(2)
            ->select();
        foreach ($list as $k => $val){
            $list[$k]['addtime'] = date('Y-m-d h:i',$val['addtime']);
        }
        $msg['content'] = json_decode($msg['content'],true);
        $msg['addtime'] = date('Y-m-d H:i');
        $msg['comments'] =$list;
        return Common::rm(1,'操作成功',['result' => [$msg]]);
    }

    //发表评论
    public function publishFire(){
        $data = $this->data;
        $new_data = [
            'addtime' => THINK_START_TIME,
            'parentID' => $data['contentID'],
            'userID' => $this->getUserID(),
            'fireID' => $data['fireID'],
            'content' => $data['content'],
            'sendUserID' => $data['sendUserID'],
        ];
        $id = Db::name('fire_comments')->insertGetId($new_data);
        if ($id){
            return Common::rm(1,'操作成功');
        }
    }

    //根据消防宝典ID得到评论
    public function getFireCommentsByFireID(){
        //得到新闻评论(10条)
        $userID = $this->getUserID();
        $list = Db::view('fire_comments',['id' => 'contentID','content','addtime','praise','past'])
            ->view('operator',['id' => 'userID','name' => 'userName','photo' => 'thumb'],'operator.id = fire_comments.userID')
            ->where('fire_comments.fireID',$this->data['fireID'])
            ->where('parentID',0)
            ->limit(10)
            ->order('addtime desc')
            ->select();
        $result = [];
        if ($list){
            foreach ($list as $k => $val){
                $result[$k]['userID'] = $val['userID'];
                $result[$k]['name'] = $val['userName'];
                $result[$k]['content'] = $val['content'];
                $result[$k]['contentID'] = $val['contentID'];
                $result[$k]['praise'] = $val['praise'];
                $result[$k]['thumb'] = $val['thumb'];
                $result[$k]['addtime'] = date('Y-m-d H:i',$val['addtime']);
                if (!empty($val['past'])){
                    //用户已经对该评论点赞
                    $a = explode(',',$val['past']);
                    foreach ($a as $key => $value){
                        if ($value == $userID){
                            $result[$k]['past'] = 1;
                            break;
                        }else{
                            $result[$k]['past'] = 0;
                        }
                    }
                }else{
                    //用户未对该评论点赞
                    $result[$k]['past'] = 0;
                }
            }
            return Common::rm(1,'操作成功',['commentsList' => array_reverse($result)]);
        }else{
            return Common::rm(-2,'客官暂时还没有任何评论哟');
        }
    }

    //根据消防宝典ID得到更多评论
    public function getMoreComments(){
        //得到新闻评论(10条)
        $userID = $this->getUserID();
        $list = Db::view('fire_comments',['id' => 'contentID','content','addtime','praise','past'])
            ->view('operator',['id' => 'userID','name' => 'userName','photo' => 'thumb'],'operator.id = fire_comments.userID')
            ->where('fire_comments.fireID',$this->data['fireID'])
            ->where('parentID',0)
            ->limit(($this->data['startPoint'] - 1) * $this->data['several'], $this->data['several'])
            ->order('addtime desc')
            ->select();
        $result = [];
        if ($list){
            foreach ($list as $k => $val){
                $result[$k]['userID'] = $val['userID'];
                $result[$k]['name'] = $val['userName'];
                $result[$k]['content'] = $val['content'];
                $result[$k]['contentID'] = $val['contentID'];
                $result[$k]['praise'] = $val['praise'];
                $result[$k]['thumb'] = $val['thumb'];
                $result[$k]['addtime'] = date('Y-m-d H:i',$val['addtime']);
                if (!empty($val['past'])){
                    //用户已经对该评论点赞
                    $a = explode(',',$val['past']);
                    foreach ($a as $key => $value){
                        if ($value == $userID){
                            $result[$k]['past'] = 1;
                            break;
                        }else{
                            $result[$k]['past'] = 0;
                        }
                    }
                }else{
                    //用户未对该评论点赞
                    $result[$k]['past'] = 0;
                }
            }
            return Common::rm(1,'操作成功',['commentsList' => array_reverse($result)]);
        }else{
            return Common::rm(-2,'客官暂时还没有任何评论哟');
        }
    }

    //得到回复
    public function getReply(){
        $data = $this->data;
        $list = Db::view('fire_comments','content,addtime,praise')
            ->view('operator',['id' => 'sendUserID','name' => 'sendUserName','photo' => 'thumb'],'operator.id = fire_comments.userID')
            ->view(['a_operator' => 'operators'],['id' => 'userID','name' => 'userName'],'operators.id = fire_comments.sendUserID')
            ->where('fire_comments.fireID',$data['fireID'])
            ->where('fire_comments.parentID',$data['contentID'])
            ->order('addtime')
            ->select();
        if ($list){
            $result = [];
            foreach ($list as $k => $val){
                $result[$k]['name'] = $val['sendUserName'].'回复'.$val['userName'];
                $result[$k]['content'] = $val['content'];
                $result[$k]['thumb'] = $val['thumb'];
                $result[$k]['addtime'] = date('Y-m-d H:i',$val['addtime']);
            }
            return Common::rm(1,'操作成功',['replyList' => $result]);
        }else{
            return Common::rm(-2,'客官暂时还没有任何回复哟');
        }

    }

    //消防宝典与我相关
    public function relatedForMe()
    {
        $userID = $this->getUserID();
        $fireID = Db::name('fire_comments')->where('userID',$userID)->whereOr('sendUserID',$userID)->distinct(true)->column('fireID');
        if (!$fireID) {
            return Common::rm(-3,'数据为空');
        }
        $comment = Db::view('fire_comments',['id' => 'commentID','parentID','userID','sendUserID','fireID','addtime','content'])
            ->view('fire_bible','title,img','fire_bible.id = fire_comments.fireID')
            ->view('operator','name,photo','operator.id = fire_comments.userID')
            ->where('fireID','in',$fireID)
            ->where('sendUserID',0)
            ->order('addtime desc')
            ->select();
        if (!$comment) {
            return Common::rm(-3,'数据为空');
        }
        $comments = Db::view('fire_comments',['id' => 'commentID','parentID','userID','sendUserID','fireID','addtime','content'])
            ->view('fire_bible','title,img','fire_bible.id = fire_comments.fireID')
            ->view('operator','name,photo','operator.id = fire_comments.userID')
            ->view(['a_operator'=>'newuser'],['name' => 'sendName','photo' => 'sendPhoto'],'newuser.id = fire_comments.sendUserID')
            ->where('fireID','in',$fireID)
            ->order('addtime desc')
            ->select();
        if ($comments) {
            $answer = Db::name('fire_comments')->where('fireID','in',$fireID)->field('id,content')->select();
            foreach ($comments as $_key => $_val) {
                foreach ($answer as $__key => $__val) {
                    if ($_val['parentID'] == $__val['id']) {
                        $comments[$_key]['sendContent'] = $__val['content'];
                    }
                }
            }
            foreach ($comments as $key => $val) {
                array_push($comment,$val);
            }
        }
        $ar = [];
        foreach ($comment as $k => $v) {
            $comment[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $ar[]  = $v['addtime'];
        }
        array_multisort($ar,SORT_DESC,$comment);
        $count = $this->data['count'];
        if ($this->data['page'] == 1) {
            $page = 0;
        } else {
            $page = ($this->data['page'] - 1) * $count;
        }
        $comment = array_slice($comment, $page, $count);
        return Common::rm(1,'操作成功',[
            'comment' => $comment
        ]);
    }
}